﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using AutoGear.BLL.Exceptions;
using AutoGear.BLL;

public partial class admin_product_review_edit : SiteAdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadReview();
        }
    }
    private void LoadReview()
    {
        try
        {
            TblProductReview review = TblProductReview.GetTblProductReviewByProductReviewId(ReviewID);
            if (review != null)
            {
                txtComment.Text = Server.HtmlDecode(review.ReviewText).Replace("<br />", "\n");
                txtCustomerEmail.Text = review.CustomerEmail;
                txtCustomerName.Text = review.CustomerName;
                txtProductName.Text = review.ProductName;
                ReviewRating.CurrentRating = Convert.ToInt32(review.RatingValue);
                chkApproved.Checked = review.IsApproved.Value;
            }
        }
        catch (ECommerceException ece)
        {
            uccResponseMessage.SetFailMessage(ece.Message);
        }
        catch (Exception)
        {
            uccResponseMessage.SetFailMessage("Error Loading Review");
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            TblProductReview review = TblProductReview.GetTblProductReviewByProductReviewId(ReviewID);
            if (review != null)
            {
                review.CustomerName = txtCustomerName.Text;
                review.CustomerEmail = txtCustomerEmail.Text;
                review.ReviewText = Server.HtmlEncode(txtComment.Text.Replace("\n", "<br />"));
                if(Session["_lastRatingValue"] != null)
                    review.RatingValue = Convert.ToInt32(Session["_lastRatingValue"].ToString());
                review.IsApproved = chkApproved.Checked;
                review.Save();

                uccResponseMessage.SetSuccessMessage("Review Updated Successfully");

                Email.SendGenericEmail(review.CustomerName, review.CustomerEmail, "Review Status Updated", String.Format("Your product review for the product <strong>{0}</strong> has been {1} by the administrator.", review.ProductName, review.IsApproved.Value ? "published" : "declined"));
            }
        }
        catch (ECommerceException ece)
        {
            uccResponseMessage.SetFailMessage(ece.Message);
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message);
            uccResponseMessage.SetFailMessage("An unknown error occured while updating the review.");
        }
    }
    protected void ReviewRating_Changed(object sender, AjaxControlToolkit.RatingEventArgs e)
    {
        Session["_lastRatingValue"] = e.Value;
    }
    private int ReviewID
    {
        get
        {
            int reviewId = 0;
            if (Request["rid"] != null && !String.IsNullOrEmpty(Request["rid"]))
                Int32.TryParse(Request["rid"].ToString(), out reviewId);
            return reviewId;
        }
    }
}